package com.ky55.util;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

public class KyCommentGenerator extends DefaultCommentGenerator {

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
        String remarks = introspectedColumn.getRemarks();
        if(StringUtility.stringHasValue(remarks)){
            field.addJavaDocLine("/**");
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            String[] lines = remarkLines;
            int length = remarkLines.length;

            for(int index = 0; index < length; ++index) {
                String remarkLine = lines[index];
                field.addJavaDocLine(" *   " + remarkLine);
            }

            field.addJavaDocLine(" */");
        }
    }


    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addJavaDocLine("/**");
        String remarks = introspectedTable.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            topLevelClass.addJavaDocLine(" * Database Table Remarks:");
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            String[] var5 = remarkLines;
            int var6 = remarkLines.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                String remarkLine = var5[var7];
                topLevelClass.addJavaDocLine(" *   " + remarkLine);
            }

            topLevelClass.addJavaDocLine(" *");

//            StringBuilder sb = new StringBuilder();

//            sb.append(introspectedTable.getFullyQualifiedTable());
//            topLevelClass.addJavaDocLine(sb.toString());
//            topLevelClass.addJavaDocLine(" */");
        }
    }

    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
        imports.add(new FullyQualifiedJavaType("javax.annotation.Generated"));
        String comment = "Source field: " + introspectedTable.getFullyQualifiedTable().toString() + "." + introspectedColumn.getActualColumnName();
        method.addAnnotation(this.getGeneratedAnnotation(comment));
    }

    private String getGeneratedAnnotation(String comment) {
        StringBuilder buffer = new StringBuilder();
        buffer.append("@Generated(");

        buffer.append(", comments=\"");
        buffer.append(comment);
        buffer.append('"');

        buffer.append(')');
        return buffer.toString();
    }


}
